package com.admin.thread;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import com.admin.bean.AdminSpBean;
import com.admin.bean.AdminSpCpBean;
import com.admin.bean.ProvinceBean;
import com.admin.dao.AdminSpCpDao;
import com.admin.dao.AdminUserDao;
import com.admin.service.AdminSpService;
import com.admin.service.ProvinceService;
import com.admin.util.BaseUtil;
import com.admin.util.BillingUtil;
import com.admin.util.SqlUtil;
import com.admin.util.db.DbOperation;

public class HourStatThread  extends Thread{

	private AdminSpCpDao adminSpCpDao;	
	private AdminSpService adminSpDao;
	
	
	
	
	
	public HourStatThread(AdminSpCpDao adminSpCpDao,AdminSpService adminSpDao) {
		this.adminSpCpDao = adminSpCpDao;
		this.adminSpDao = adminSpDao;
	}

	public static long SLEEP_TIME = 1000*10;
	
	private Date date;
	
	private boolean started = true;
	
	
	public boolean isStarted() {
		return started;
	}

	public void setStarted(boolean started) {
		this.started = started;
	}

	@Override
	public void run() {
		
		System.out.println("run");
		
		try {
			Thread.sleep(5000);
		} catch (InterruptedException e) {
			e.printStackTrace();
			return;
		}
		
		while(started){
			try {
				Thread.sleep(SLEEP_TIME);
			} catch (InterruptedException e) {
				return;
			}
			//task();
			try{
				if(started){
					boolean flag = false;
					date = new Date();
					System.out.println("min:"+date.getMinutes());
					if(date.getMinutes()>=50) {
					//if(true) {	
						System.out.println("start every hour stat task..");
						try {
							task();
						} catch (Exception e) {
							e.printStackTrace();
							try {
								Thread.sleep(60*1000*10);
							} catch (Exception e1) {
								e1.printStackTrace();
								return;
							} finally {

							}
							continue;
						} finally {
							try {
								Thread.sleep(60*1000*10);
							} catch (Exception e) {
								// TODO: handle exception
								e.printStackTrace();
								return;
							} finally {

							}
						}
						
						flag = true;
					} else {
						flag = false;
						System.out.println("sleep 10 mins");
						try {
							Thread.sleep(60*1000*10);
						} catch (Exception e) {
							// TODO: handle exception
							e.printStackTrace();
							return;
						} finally {

						}
					}
				}
			}catch(Exception e){
				e.printStackTrace();
			}
		}
	}

	public void task(){
//		List<AdminUserBean> cpList = adminUserDao.findCp();
//		
//		for (AdminUserBean adminUserBean : cpList) {
		String recvTableName = BillingUtil.getStatRecvHour();
		String sendTableName = BillingUtil.getStatSendHour();
			try {
				List<AdminSpCpBean> spcpList = adminSpCpDao.findAll();
				
				for (AdminSpCpBean adminSpCpBean : spcpList) {
					
					//System.out.println("command:"+adminSpCpBean.getSpcommand());
					
					
					AdminSpBean adminSpBean = adminSpDao.findAdminSpById(adminSpCpBean.getSpid());
					//System.out.println("province:"+adminSpBean.getProvince());
					if(adminSpBean!=null&&adminSpBean.getBussiness().equals("SMS")) {
						String[] provinces = adminSpBean.getSpprovince().split(",");
						List<Integer> provinceIds = new ArrayList<Integer>();
						for (String string : provinces) {
							int provinceId = BaseUtil.stringToInt(string);
							provinceIds.add(new Integer(provinceId));
						}
						
						
						provinceIds.add(new Integer(0));
						for (Integer integer : provinceIds) {
							
							StringBuilder sbmoUser = new StringBuilder("select count(distinct org) from "+recvTableName+" where province = ");
							sbmoUser.append(integer);
							sbmoUser.append(" and dest = '");
							sbmoUser.append(adminSpCpBean.getSpcode());
							sbmoUser.append("' and body like '");
							sbmoUser.append(adminSpCpBean.getSpcommand().replace("*", ""));
							sbmoUser.append("%' and tm > Convert(datetime, CONVERT(varchar(13),DateAdd(hour,-1,getDate()),120)+':00:00') and tm < Convert(datetime, CONVERT(varchar(13),DateAdd(hour,0,getDate()),120)+':00:00')");
							
							//System.out.println("mo:"+sbmo.toString());
							String cpmoUserSql = sbmoUser.toString()+" and ischeck = 0";
							
							int moUser = SqlUtil.getIntResult(sbmoUser.toString(), DbOperation.DB_MUZHI);
							int cpmoUser = SqlUtil.getIntResult(cpmoUserSql, DbOperation.DB_MUZHI);
							
							
							
							
							StringBuilder sbmo = new StringBuilder("select count(distinct linkid) from "+recvTableName+" where province = ");
							sbmo.append(integer);
							sbmo.append(" and dest = '");
							sbmo.append(adminSpCpBean.getSpcode());
							sbmo.append("' and body like '");
							sbmo.append(adminSpCpBean.getSpcommand().replace("*", ""));
							sbmo.append("%' and tm > Convert(datetime, CONVERT(varchar(13),DateAdd(hour,-1,getDate()),120)+':00:00') and tm < Convert(datetime, CONVERT(varchar(13),DateAdd(hour,0,getDate()),120)+':00:00')");
							
							String cpmoSql = sbmo.toString()+" and ischeck = 0";
							
							
							int mo = SqlUtil.getIntResult(sbmo.toString(), DbOperation.DB_MUZHI);
//							if(integer == 17) {
//								System.out.println("**********");
//								System.out.println("mosql"+sbmo.toString());
//								System.out.println("mo:"+mo);
//								
//							}
							
							int cpmo = SqlUtil.getIntResult(cpmoSql, DbOperation.DB_MUZHI);
							//System.out.println("cpmo:"+cpmo);
							
							StringBuilder sbmt = new StringBuilder("select count(distinct linkid) from "+sendTableName+" where province = ");
							sbmt.append(integer);
							sbmt.append(" and dest = '");
							sbmt.append(adminSpCpBean.getSpcode());
							sbmt.append("' and body like '");
							sbmt.append(adminSpCpBean.getSpcommand().replace("*", ""));
							sbmt.append("%'  and tm > Convert(datetime, CONVERT(varchar(13),DateAdd(hour,-1,getDate()),120)+':00:00') and tm < Convert(datetime, CONVERT(varchar(13),DateAdd(hour,0,getDate()),120)+':00:00')");
							
							String cpmtSql = sbmt.toString()+" and ischeck = 0";
							
							
							int mt = SqlUtil.getIntResult(sbmt.toString(),DbOperation.DB_MUZHI);
							//System.out.println("mt:"+mt);
							int cpmt = SqlUtil.getIntResult(cpmtSql, DbOperation.DB_MUZHI);
							//System.out.println("cpmt:"+cpmt);
							
							
							StringBuilder sbstatmt = new StringBuilder("select count(distinct linkid) from "+sendTableName+" where province = ");
							sbstatmt.append(integer);
							sbstatmt.append(" and dest = '");
							sbstatmt.append(adminSpCpBean.getSpcode());
							sbstatmt.append("' and body like '");
							sbstatmt.append(adminSpCpBean.getSpcommand().replace("*", ""));
							sbstatmt.append("%' and stat = 'DELIVRD' and tm > Convert(datetime, CONVERT(varchar(13),DateAdd(hour,-1,getDate()),120)+':00:00') and tm < Convert(datetime, CONVERT(varchar(13),DateAdd(hour,0,getDate()),120)+':00:00')");
							
							String cpstatmtSql = sbstatmt.toString()+" and ischeck = 0";
							
							
							
							int statmt = SqlUtil.getIntResult(sbmt.toString(),DbOperation.DB_MUZHI);
							//System.out.println("statmt:"+statmt);
							int cpstatmt = SqlUtil.getIntResult(cpstatmtSql,DbOperation.DB_MUZHI);
							//System.out.println("cpstatmt:"+cpstatmt);
							
							if(mo>0||mt>0||statmt>0) {						
								//StringBuilder statsql = new StringBuilder("insert into T_co_stat_fee_detail_day_2(dt,spid,cityid,cpid,mo,mt,infofee,income,statmt,statfee,statincome)  values(");
								StringBuilder statsql = new StringBuilder("insert into T_co_stat_fee_detail(dt,gatewayid,cityid,channel,moUser,mo,mt,infofee,income,statmt,statfee,statincome,cpMoUser,cpMo,cpMt,cpInfofee,cpIncome,cpStatmt,cpStatfee,cpStatincome,province)  values(");
								statsql.append("CONVERT(varchar(13),DateAdd(hour,-1,getDate()),120),");
								statsql.append(adminSpCpBean.getSpid());
								statsql.append(",");
								statsql.append(integer);
								statsql.append(",");
								statsql.append(adminSpCpBean.getCpid());
								statsql.append(",");
								statsql.append(moUser);
								statsql.append(",");
								statsql.append(mo);
								statsql.append(",");
								statsql.append(mt);
								statsql.append(",");
								statsql.append(mo*adminSpBean.getSpfee());
								statsql.append(",");
								statsql.append(mo*adminSpBean.getSpfee()*0.85);
								statsql.append(",");
								statsql.append(statmt);
								statsql.append(",");
								statsql.append(statmt*adminSpBean.getSpfee());
								statsql.append(",");
								statsql.append(statmt*adminSpBean.getSpfee()*0.85);
								statsql.append(",");
								
								
								statsql.append(cpmoUser);
								statsql.append(",");
								statsql.append(cpmo);
								statsql.append(",");
								statsql.append(cpmt);
								statsql.append(",");
								statsql.append(cpmo*adminSpBean.getSpfee());
								statsql.append(",");
								statsql.append(cpmo*adminSpBean.getSpfee()*0.85);
								statsql.append(",");
								statsql.append(cpstatmt);
								statsql.append(",");
								statsql.append(cpstatmt*adminSpBean.getSpfee());
								statsql.append(",");
								statsql.append(cpstatmt*adminSpBean.getSpfee()*0.85);
								statsql.append(",");
								statsql.append(integer);
								statsql.append(")");
								
								System.out.println(statsql.toString());
								SqlUtil.executeUpdate(statsql.toString(), DbOperation.DB_MUZHI);
							} else {
								//System.out.println("zero");
							}
						}
					}
				}
				
			} catch (Exception e) {
				// TODO: handle exception
				e.printStackTrace();
			} finally {

			}
//			
//		}
		
	}
}
